package day2test1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;

public class Funcation {
	// 1,通过system. out提示信息,采用scanner录入学生信息,保存至集合。
	// 2,查看全部学生信息。
	// 3,按学生姓名查询该学生信息。
	// 4,创建学生类,记录保存至集合。
	// 5,添加专业,按专业查询学生信息

	static ArrayList<Student> studentArrayList = new ArrayList<>();
	static Scanner scanner = new Scanner(System.in);
 
	/*
	 * 初始化
	 */


   
	
	//写文件
	
	public static void WriteTxt(ArrayList<Student> studentArrayList) throws Exception {
		FileOutputStream fos = new FileOutputStream("student.txt");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(studentArrayList);
		oos.flush();
		oos.close();
	}
	//读文件
	public static ArrayList<Student> ReadTxt() throws Exception {
		FileInputStream fis = new FileInputStream("student.txt");

		ObjectInputStream oin = new ObjectInputStream(fis);
	
		studentArrayList = (ArrayList<Student>) oin.readObject();
		oin.close();
		fis.close();
		return studentArrayList;
		
	}
	//初始化
	public void   initialize() throws IOException {
		FileOutputStream fos = new FileOutputStream("student.txt");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(studentArrayList);
		oos.flush();
		oos.close(); 
	}
	
	/*
	 * 录入学生信息
	 */

	public void addStudent() throws Exception {

		while (true) {

			System.out.println("请输入学生id");
			int id = Integer.parseInt(scanner.nextLine());
			System.out.println("请输入学生姓名");
			String name = scanner.nextLine();
			System.out.println("请输入学生年龄");
			int age = Integer.parseInt(scanner.nextLine());
			System.out.println("请输入学生性别");
			String sex = scanner.nextLine();
			System.out.println("请输入学生班级"); 
			String classGrade = scanner.nextLine();
			System.out.println("请输入学生专业");
			String major = scanner.nextLine();
			
			Student student = new Student(id, name, age, sex, classGrade, major);
			
			
		
		
			studentArrayList = ReadTxt();
		

			studentArrayList.add(student);

			System.out.println("继续录入请输入'1',录入结束请输入'0'"); 
			
			WriteTxt(studentArrayList);

			int flag = Integer.parseInt(scanner.nextLine());
			if (flag == 0) {
				break;
			}
		}

//		System.out.println(studentArrayList.get(0));

	}

	// 查看全部学生信息。

	public void findAll() throws Exception {
		int i = 1;
		studentArrayList = ReadTxt();
		
		for (Student student : studentArrayList) {
			int id = student.getId();
			String name = student.getName();
			int age = student.getAge();
			String sex = student.getSex();
			String classGrade = student.getClass_grade();
			String major = student.getMajor();

			System.out.println(i + " 学号=" + id + ", 姓名=" + name + ", 年龄=" + age + ", 性别=" + sex + ", 班级=" + classGrade
					+ "专业" + major);
			i++;

		}

	}

	// 按学生姓名查询该学生信息
	public void findbyName() throws Exception {
		studentArrayList = ReadTxt();
		

		while (true) {
			System.out.println("请输入学生姓名:");
			String nameString = scanner.nextLine();
			for (Student student : studentArrayList) {
				if (nameString.equals(student.getName())) {
					int id = student.getId();
					String name = student.getName();
					int age = student.getAge();
					String sex = student.getSex();
					String classGrade = student.getClass_grade();
					String major = student.getMajor();
					System.out.println(" 学号: " + id + ", 姓名: " + name + ", 年龄: " + age + ", 性别: " + sex + ", 班级 :"
							+ classGrade + "专业: " + major);
				}
			}
			System.out.println("继续查询请输入1，结束查询请输入0");
			int flag = Integer.parseInt(scanner.nextLine());
			if (flag == 0) {
				break;
			}
		}
	}

	// 添加学生专业
	public void addMajor() throws Exception {
		studentArrayList = ReadTxt();


		while (true) {
			System.out.println("请输入要添加的学生的学号");
			int id = Integer.parseInt(scanner.nextLine());

			System.out.println("请输入该学生的专业");
			String major = scanner.nextLine();
			for (Student student : studentArrayList) {
				if (student.getId() == id) {
					student.setMajor(major);
				}
			}
			
			WriteTxt(studentArrayList);

			System.out.println("继续添加请输入'1',结束添加请输入'0'");
			int flag = Integer.parseInt(scanner.nextLine());
			if (flag == 0) {
				break;
			}

		}
	
	}

	// 按照专业查询学生
	public void findbyMajor() throws Exception {
		studentArrayList = ReadTxt();
		
		while (true) {
			System.out.println("请输入学生专业:");
			String major = scanner.nextLine();
			for (Student student : studentArrayList) {
				if (major.equals(student.getMajor())) {
					int id = student.getId();
					String name = student.getName();
					int age = student.getAge();
					String sex = student.getSex();
					String classGrade = student.getClass_grade();

					System.out.println(" 学号=" + id + ", 姓名=" + name + ", 年龄=" + age + ", 性别=" + sex + ", 班级="
							+ classGrade + "专业" + major);
				}
			}
			System.out.println("继续查询请输入1，结束查询请输入0");
			int flag = Integer.parseInt(scanner.nextLine());
			if (flag == 0) {
				break;
			}
		}
		
	}

	// 按照学号进行排序(由低到高)---------- (扩展)
//	public ArrayList<Student> sortByid() {
//		for (int i = 0; i < studentArrayList.size(); i++) {
//			for (int j = 0; j < studentArrayList.size() - 1; j++) {
//				if (studentArrayList.get(j).getId() < studentArrayList.get(j + i).getId()) {
//					Student tempStudent = studentArrayList.get(j);
//					Student tempStudent2 = studentArrayList.get(j + 1);
//					studentArrayList.set(j, tempStudent2);
//					studentArrayList.set(j + 1, tempStudent);
//				}
//			}
//		}
//		return studentArrayList;
//	}
	// 按照学号进行排序(由低到高)---------- (扩展)TreeSet
	public void sortByid() throws Exception {
		studentArrayList = ReadTxt();
		
		
		TreeSet<Student> studentTreeSet = new TreeSet<Student>();
		for (Student student : studentArrayList) {
			studentTreeSet.add(student);
		}

		for (Object object : studentTreeSet) {
			Student student = (Student) object;
			System.out.println("学号:" + student.getId() + " 姓名：" + student.getName() + " 年龄:" + student.getAge() + " 性别:"
					+ student.getSex() + " 班级:" + student.getClass_grade() + " 专业：" + student.getMajor());
		}
	
	}

	// 菜单功能展示
	public void menuShow() {
		System.out.println("******** 0.初始化信息（首次使用必须初始化信息）        ********");
		System.out.println("******** 1.添加学生信息        ********");
		System.out.println("******** 2.查找所有学生信息     ********");
		System.out.println("******** 3.输入姓名查找学生信息  ********");
		System.out.println("******** 4.添加学生专业信息     ********");
		System.out.println("******** 5.输入专业查找学生信息  ********");
		System.out.println("******** 6.通过学号对学生信息排序 ********");
		

	}

	// 菜单功能选择
	public void menuChoose() throws Exception {
		while (true) {
			menuShow();
			System.out.println("请输入你要选择的功能");
			int choose = Integer.parseInt(scanner.nextLine());
			switch (choose) {
			case 0: {
				initialize();
				break;
				
			}
			case 1: {
				addStudent();
				break;

			}
			case 2: {
				findAll();
				break;
			}
			case 3: {
				findbyName();
				break;
			}
			case 4: {
				addMajor();
				break;
			}
			case 5: {
				findbyMajor();
				break;
			}
			case 6: {
				sortByid();
				break;
			}

			default:
				throw new IllegalArgumentException("输入错误");
			}
			System.out.println("退出请输入'#',继续输入请输入任意");
			String flag = scanner.nextLine();
			if ("#".equals(flag)) {
				break;
			}
		}
	}
}
